package com.heima.wemedia.controller.v1;

import com.heima.model.admin.dtos.SensitivePageReqDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.service.WmChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/channel")
public class WmchannelController {

    @Autowired
    private WmChannelService wmChannelService;

    @GetMapping("/channels")
    public ResponseResult findAll(){
        return wmChannelService.findAll();
    }

    @PostMapping("/list")
    public ResponseResult list(@RequestBody SensitivePageReqDto dto){
        return wmChannelService.listchannel(dto);
    }

    @PostMapping("/save")
    public ResponseResult save(@RequestBody WmChannel dto){
        WmChannel wmChannel = wmChannelService.getById(dto.getId());
        if (wmChannel != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"数据已存在");
        }
        boolean flag = wmChannelService.save(dto);
        if (!flag){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"添加失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody WmChannel dto){
        boolean flag = wmChannelService.updateById(dto);
        if (!flag){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"修改失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @GetMapping("/del/{id}")
    public ResponseResult deleteById(@PathVariable Integer id){
        boolean flag = wmChannelService.removeById(id);
        if (!flag){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"删除失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}